package ModuleRun;

import LiftEngine.Lift;
import Module.ModuleClient;
import Module.ModuleWorker;
import Server.ClientList;
import Const.*;
import Module.ModuleReceiver;
import Module.ModuleSender;
import java.net.InetAddress;
import java.net.Socket;

/**
 *
 * @author q530
 */
public class LiftModuleMain {

    ModuleClient moduleClient;
    ModuleSender moduleSender;
    ModuleReceiver moduleReciver;

    private Socket socket;
    private InetAddress destAddress;
    private int port = ServerConst.PortModule;
    private String host = "localhost";

    public LiftModuleMain(int port, String host) {
        // zczytanie wywolan progrmau czyli host i port
        this.port = port;
        this.host = host;
        
        
        // inicjalizacja socketa
        initSocket();


        //inicjalizacja modulu
        Lift lift = new Lift();
        
        moduleClient = new ModuleClient(this.socket,0,port, lift, ModuleConst.TypeLift);
        this.moduleClient.setConnected(true);
        this.moduleClient.setPort(port);
        this.moduleClient.setHostname(host);




        moduleReciver = new ModuleReceiver(moduleClient);
        moduleReciver.start();




        moduleSender = new ModuleSender(moduleClient);
        moduleSender.start();


        ModuleWorker mWorker = new ModuleWorker("AlgorithmThread", moduleClient);
        mWorker.start();

    }
  public InetAddress getDestAddress() {
        return destAddress;
    }

    public int getPort() {
        return port;
    }

    public void initSocket()
    {
        
        try{
            destAddress = InetAddress.getByName(host); // Destination address
            int destPort = getPort();              // Destination port
            this.socket = new Socket(destAddress, destPort);
        }
        catch (Exception ex)
        {
            System.out.println(ex.getClass() +  " \t" + ex.getMessage());
        }
        
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String hostname = "";
        int port = 0;

        if ( args.length < 1 ){
            hostname = "localhost";
        }
        else{
            hostname = args[0];
        }

        if ( args.length < 2 ){
            port = ServerConst.PortModule;
        }
        else{
            port = new Integer(args[1]);
        }

        LiftModuleMain liftModuleMain = new LiftModuleMain(port, hostname);
        
        System.out.println("Welcome to LIFT SIMULATOR STEERING MODULE!");
    }
}
